package embeddedTomcat;
//http://www.theserverside.com/discussions/thread.tss?thread_id=30625
import java.io.File;

import java.net.InetAddress;

import org.apache.catalina.Context;

import org.apache.catalina.Engine;

import org.apache.catalina.Host;

import org.apache.catalina.startup.Embedded;

public class EmbeddedTomcat1 {

	private String contextPath = null;
	private String hostName = null;
	private String catalinaHomePath = null;
	private int port = 8080;
	private Embedded embedded = null;
	private Host host = null;
	public EmbeddedTomcat1(String contextPath, String catalinaHomePath,
			String hostName, int port)
	{
		this.contextPath = contextPath;
		this.catalinaHomePath = catalinaHomePath;
		this.hostName = hostName;
		this.port = port;
	}

	/**
	 * 
	 * This method Starts the Tomcat server.
	 */

	public void startTomcat() throws Exception {

		// Create an embedded server
		embedded = new Embedded();
		Engine engine = null;

		// System.setProperty("catalina.home", getPath());
		embedded.setCatalinaHome(catalinaHomePath);

		// Create an engine
		engine = embedded.createEngine();

		// Create a default virtual host
		host = embedded.createHost(hostName, contextPath + "/webapps");
		engine.addChild(host);
		engine.setDefaultHost(host.getName());

		// Create the ROOT context
		Context rootCxt = embedded.createContext("", contextPath
				+ "/webapps/ROOT");

//		commented out by pjs
//		Context manageCxt = embedded.createContext("/manager", contextPath
//				+ "/webapps/manager");

		// Create your own context

//		Context scoreCxt = embedded.createContext("/vmm", contextPath
//				+ "/webapps/vmm");

		rootCxt.setPrivileged(true);
		host.addChild(rootCxt);

//		commented out by pjs
//		host.addChild(manageCxt);

//		host.addChild(scoreCxt);

		// Install the assembled container hierarchy
		embedded.addEngine(engine);

		// Assemble and install a default HTTP connector
		embedded.addConnector(embedded.createConnector(InetAddress
				.getByName(null), port, false));

		// Start the embedded server
		embedded.start();

	}

	/**
	 * 
	 * This method Stops the Tomcat server.
	 */

	public void stopTomcat() throws Exception {

		// Stop the embedded server
		embedded.stop();

	}

	public static void main(String args[]) {
		try {
			String a=new File("./").getCanonicalPath();
			String contextPath = a+File.separator+"embeddedTomcat";
//			String catalinaHomePath = (new File("./conf")).getCanonicalPath();
			String catalinaHomePath = a+File.separator+"embeddedTomcat"+File.separator+"conf";
			String hostName = "localhost";
			int port = 8080;
			System.out.println("contextPath:" + contextPath);
			System.out.println("catalinaHomePath:" + catalinaHomePath);
			EmbeddedTomcat1 tomcat = new EmbeddedTomcat1(contextPath,
					catalinaHomePath, hostName, port);
			tomcat.startTomcat();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}

}

//this url provided solution has problem
//http://affy.blogspot.com/2006/10/embedded-tomcat-5517-eclipse-project.html
